package com.xbongbong.product.abstracts;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.help.log.InsertDataLogHelp;
import com.xbongbong.paas.business.rule.FormDataActiveRuleHelp;
import com.xbongbong.paas.data.service.CabinetFileService;
import com.xbongbong.paas.data.service.RichTextService;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.lowcode.LowCodeExecuteHelp;
import com.xbongbong.paas.pojo.dto.FormDataUpdateDTO;
import com.xbongbong.paas.pojo.vo.FormDataUpdateVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.util.FormDataUtil;
import com.xbongbong.parent.abstracts.AbstractFormDataUpdateService;
import com.xbongbong.pro.formdata.pojo.SaasNeedRedundantAttrPojo;
import com.xbongbong.pro.formdata.pojo.vo.BeforeUpdateVO;
import com.xbongbong.pro.formdata.pojo.vo.UpdateVO;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.paas.service.workflow.trigger.WorkflowFormDataOperationService;
import com.xbongbong.workflow.domain.entity.WorkflowBackupDataEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 产品抽象类，实现产品中的公用方法
 * @author 吴峰
 * @date 2022/01/05 10:12
 */
@Component
public abstract class AbstractProductUpdateDataService extends AbstractFormDataUpdateService {

    @Resource
    private LowCodeExecuteHelp lowCodeExecuteHelp;
    @Resource
    private WorkflowFormDataOperationService workflowFormDataOperationService;
    @Resource
    private FormDataActiveRuleHelp formDataActiveRuleHelp;
    @Resource
    private InsertDataLogHelp insertDataLogHelp;
    @Resource
    private CabinetFileService cabinetFileService;
    @Resource
    private RichTextService richTextService;

    /**
     * 更新表单富文本
     *
     * @param formDataUpdateDTO
     * @param richTextData
     * @return
     */
    protected Integer updateFormRichText(FormDataUpdateDTO formDataUpdateDTO, Object richTextData) {
        return richTextService.updateFormRichText(formDataUpdateDTO, richTextData);
    }

    /**
     * 移除富文本
     *
     * @param data Json数据
     * @return 富文本数据
     */
    protected Object removeRichText(JSONObject data){
        return FormDataUtil.removeRichText(data);
    }

    /**
     * 备份富文本
     *
     * @param workflowBackupDataEntity
     * @param businessType
     * @param saasMark
     */
    protected void backupRichText(WorkflowBackupDataEntity workflowBackupDataEntity, Integer businessType, Integer saasMark){
        richTextService.backupRichText(workflowBackupDataEntity, businessType, saasMark);
    }

    @Override
    public void saveFile(Map<String, FieldAttrEntity> explainMap, PaasFormDataEntity paasFormDataEntity, Integer businessType, UserVO userVO) throws XbbException {
        String corpid = userVO.getCorpid();
        Long dataId = paasFormDataEntity.getDataId();
        //文件柜数据保存
        cabinetFileService.saveDataCabinetFile(explainMap, paasFormDataEntity, corpid, dataId, businessType, userVO.getUserId());

    }

    @Override
    public boolean isEnableNewWorkflow(FormDataUpdateDTO formDataUpdateDTO, Map explainMap, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormData) throws XbbException {
        return workflowFormDataOperationService.isEnableNewWorkflow(formDataUpdateDTO, explainMap, paasFormDataEntity, oldPaasFormData);
    }


    @Override
    public Integer startProcess(FormDataUpdateDTO formDataUpdateDTO, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity, PaasFormEntityExt paasFormEntityExt, SaasNeedRedundantAttrPojo saasNeedRedundantAttrPojo) throws XbbException {
        return 1;
    }

    @Override
    public void workflowUpdateTrigger(FormDataUpdateDTO formDataUpdateDTO, BeforeUpdateVO beforeSaveVO, FormDataUpdateVO formDataSaveVO, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity) throws XbbException {
        workflowFormDataOperationService.workflowUpdateTrigger(formDataUpdateDTO, beforeSaveVO, formDataSaveVO);
    }


    public void saveLogger(FormDataUpdateDTO formDataUpdateDTO, PaasFormDataEntity paasFormDataEntity, RedundantTemplateTypeEnum redundantTemplateTypeEnum, String logMemo,
                           String opObjectName, OperateModuleTypeEnum operateModuleTypeEnum, UpdateVO updateVO,
                           BeforeUpdateVO beforeUpdateVO, OperateTypeEnum operateTypeEnum, IndexTypeEnum indexTypeEnum) {
        insertDataLogHelp.saveLogger(formDataUpdateDTO, paasFormDataEntity, redundantTemplateTypeEnum, logMemo, opObjectName, operateModuleTypeEnum, updateVO, beforeUpdateVO, operateTypeEnum, indexTypeEnum);
    }
    @Override
    public void lowCodeExecuteService(FormDataUpdateDTO formDataUpdateDTO, UpdateVO updateVO, BeforeUpdateVO beforeUpdateVO) throws XbbException {
        lowCodeExecuteHelp.lowCodeExecuteService(formDataUpdateDTO, updateVO, beforeUpdateVO);
    }


    @Override
    public void activeRule(FormDataUpdateDTO formDataUpdateDTO, BeforeUpdateVO beforeUpdateVO, UpdateVO updateVO) throws XbbException {
        formDataActiveRuleHelp.activeRule(formDataUpdateDTO, beforeUpdateVO, updateVO);
    }
}
